<template>
  <!-- 所有内容必须包裹在一个根元素内 -->
  <view class="container">
    <!-- 顶部导航栏 -->
    <view class="custom-nav">
      <!-- 左上角返回按钮 -->
      <view class="nav-back" @click="navigateBack">
        <image class="back-icon" :src="'/static/icon/back.png'" mode="aspectFit" />
      </view>

      <!-- 中间标题 -->
      <view class="nav-title">{{ categoryName }}</view>

      <!-- 右上角操作按钮 -->
      <view class="nav-actions">
        <image class="search-icon" :src="'/static/icon/search.png'" mode="aspectFit" />
        <image class="menu-icon" :src="'/static/icon/more.png'" mode="aspectFit" />
      </view>
    </view>

    <view class="category-buttons">
      <view v-for="(category, index) in categories" :key="category.id" class="category-btn"
        :class="{ active: index === selectedCategoryIndex }"
        @click="handleCategoryChange({ detail: { value: index } })">
        {{ category.name }}
      </view>
    </view>

    <!-- 全部作品板块 -->
    <view class="section">
      <view class="section-title">全部作品</view>
      <view class="book-list">
        <view v-for="book in books" :key="book.id" class="book-item" @click="navigateToDetail(book.id)">
          <image class="book-cover" :src="book.cover" mode="aspectFill" />
          <view class="book-info">
            <text class="book-title">{{ book.title }}</text>
            <text class="book-author">{{ book.author }}</text>
            <text class="book-desc">{{ book.description }}</text>
            <!-- 新增状态显示区域 -->
            <view class="book-meta">
              <text class="book-status" :class="book.status">
                {{ getStatusText(book.status) }}
              </text>
              <text class="book-distance">
                {{ book.distance }}
              </text>
            </view>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import { getBookList } from '@/api/book'
import { onMounted, ref } from 'vue';

export default {
  data() {
    return {
      categoryId: null,
      selectedCategoryIndex: 0,
      categories: [
        { id: 1, name: '文学' },
        { id: 2, name: '科技' },
        { id: 3, name: '历史' },
        { id: 4, name: '艺术' }
      ],
      books: [],
    };
  },

  computed: {
    categoryName() {
      const category = this.categories.find(c => c.id === this.categoryId);
      return category ? category.name : '未知分类';
    },
  },
  onLoad(options) {
    if (options.categoryId) {
      this.categoryId = Number(options.categoryId);
      this.selectedCategoryIndex = this.categories.findIndex(
        c => c.id === this.categoryId
      );
    }
    this.loadData(this.categoryId); // Now correctly references the method
  },
  methods: {
    navigateBack() {
      uni.navigateBack({
        delta: 1
      });
    },
    async loadData(categoryId) {
        try {
          const res = await getBookList({ category: categoryId });
          this.books = res.data.list;
        } catch (error) {
          console.error('加载图书失败:', error);
        }
      },
    handleCategoryChange(e) {
      const index = e.detail.value;
      this.categoryId = this.categories[index].id;
      this.selectedCategoryIndex = index;
      this.loadData(this.categoryId); // 切换分类时传递新的分类ID
    },
    navigateToDetail(bookId) {
      uni.navigateTo({
        url: `/pages/book/detail/index?id=${bookId}`
      });
    },
    // 新增状态文本转换方法
    getStatusText(status) {
      const statusMap = {
        available: '可借阅',
        borrowed: '已借出',
        reserved: '已预约',
      };
      return statusMap[status] || status;
    },
  }
};

</script>

<style scoped>
.container {
  padding: 20rpx;
}

.category-header {
  text-align: center;
  padding: 40rpx 0;
  background-color: #f8f9fa;
  border-radius: 16rpx;
  margin-bottom: 30rpx;
}

.category-name {
  font-size: 48rpx;
  font-weight: bold;
  color: #2c3e50;
}

.section {
  margin-bottom: 40rpx;
  margin-top: 30rpx;
}

.section-title {
  font-size: 36rpx;
  color: #2c3e50;
  margin-bottom: 20rpx;
  padding-bottom: 10rpx;
  border-bottom: 4rpx solid #e0e0e0;
}

.book-list {
  display: flex;
  flex-direction: column;
  gap: 30rpx;
}

.book-item {
  display: flex;
  background-color: #fff;
  border-radius: 16rpx;
  overflow: hidden;
  box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.1);
}

.book-cover {
  width: 200rpx;
  height: 280rpx;
}

.book-info {
  flex: 1;
  padding: 30rpx;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.book-title {
  font-size: 32rpx;
  font-weight: bold;
  color: #2c3e50;
}

.book-author {
  font-size: 28rpx;
  color: #666;
  margin-top: 10rpx;
}

.book-desc {
  font-size: 28rpx;
  color: #999;
  margin-top: 10rpx;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

/* 导航栏整体样式 */
.custom-nav {
  display: flex;
  align-items: center;
  height: 100rpx;
  background-color: #ffffff;
  position: sticky;
  top: 0;
  z-index: 999;

}

/* 左上角返回按钮 */
.nav-back {
  width: 60rpx;
  /* 预留按钮点击区域 */
}

.nav-back image {
  width: 40rpx;
  /* 图片自适应 */
  height: 40rpx;
  /* 图片自适应 */
}

/* 中间标题 */
.nav-title {
  flex: 1;
  text-align: center;
  font-size: 40rpx;
  font-weight: bold;
  color: #2c3e50;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* 右上角操作按钮组 */
.nav-actions {
  display: flex;
  gap: 30rpx;
}

/* 单个操作按钮 */
.nav-action-btn {
  font-size: 28rpx;
  color: #666;
  white-space: nowrap;
}

/* 分类按钮容器 */
.category-buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 16rpx;
  /* 增大按钮间距 */
  padding: 24rpx 0;
  /* 调整内边距 */
  margin: 0 -16rpx;
  /* 抵消container的padding，让按钮能顶满父容器宽度 */
}

/* 单个分类按钮 */
.category-btn {
  padding: 12rpx 24rpx;
  /* 增大按钮内边距 */
  margin-left: 30rpx;
  border: 2rpx solid #e5e7eb;
  /* 添加浅灰色边框 */
  border-radius: 24rpx;
  /* 圆角按钮 */
  font-size: 28rpx;
  color: #4b5563;
  background-color: #ffffff;
  cursor: pointer;
  transition: all 0.2s;
  /* 添加过渡动画 */
}

/* 选中状态按钮 */
.category-btn.active {
  border-color: #4ade80;
  /* 绿色边框 */
  background-color: #f0fdf4;
  /* 浅绿色背景 */
  color: #22c55e;
  /* 绿色文字 */
  font-weight: 600;
  /* 加粗文字 */
}

/* 限制导航栏图标大小 */
.search-icon,
.menu-icon {
  width: 45rpx;
  /* 固定图标宽度 */
  height: 45rpx;
  /* 固定图标高度 */
}

/* 调整操作按钮与图标的对齐 */
.nav-actions {
  align-items: center;
  /* 垂直居中对齐 */
}
</style>


<style scoped>
.book-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 20rpx;
}

.book-status {
  font-size: 24rpx;
  padding: 4rpx 12rpx;
  border-radius: 4rpx;
}

.book-status.available {
  background: #e8f5e9;
  color: #4caf50;
}

.book-status.borrowed {
  background: #fff3e0;
  color: #ff9800;
}

.book-status.reserved {
  background: #e3f2fd;
  color: #2196f3;
}

.book-distance {
  font-size: 24rpx;
  color: #999;
}
</style>